package com.pandai.leetcode;

public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode slow = dummyNode;
        ListNode quick = dummyNode;
        // 先让q指针走n步
        for(int i=0;i<n;i++){
            quick=quick.next;
        }
        // 再让s和q同时走size-n步，size是链表长度，此时s位于倒数第n个结点的前一个结点
        while(quick.next !=null){
            slow = slow.next;
            quick = quick.next;
        }
        // 进行删除操作
        if(slow.next!=null){
            slow.next = slow.next.next;
        }
        return dummyNode.next;
    }

}
